package com.beyondsoft.sabg.edu.controller.dataanalysis;

import com.beyondsoft.sabg.edu.model.dto.DataSourceConfigDto;
import com.beyondsoft.sabg.edu.model.vo.core.SimpleResponseVO;
import com.beyondsoft.sabg.edu.service.dataanalysis.DataSourceService;
import com.beyondsoft.sabg.edu.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 数据源控制器
 */
@RestController
@RequestMapping("/api/dataanalysis/datasource")
@Slf4j
public class DataSourceController {

    @Autowired
    private DataSourceService dataSourceService;

    /**
     * 创建数据源授权
     */
    @PostMapping("/authorization")
    public SimpleResponseVO createDatasourceAuthorization(@Valid @RequestBody DataSourceConfigDto config) {
        log.info("创建数据源授权请求，URL：{}", config.getUrl());
        
        try {
            String result = dataSourceService.createDatasourceAuthorization(config);
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("创建数据源授权失败", e);
            return ResponseUtil.failed("创建数据源授权失败：" + e.getMessage());
        }
    }

    /**
     * 同步远程数据表
     */
    @PostMapping("/sync-tables")
    public SimpleResponseVO syncRemoteTables(@RequestBody List<String> tableNames) {
        log.info("同步数据表请求，表数量：{}", tableNames.size());
        
        try {
            String result = dataSourceService.syncRemoteTables(tableNames);
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("同步数据表失败", e);
            return ResponseUtil.failed("同步数据表失败：" + e.getMessage());
        }
    }

    /**
     * 获取数据表合集
     */
    @GetMapping("/tables")
    public SimpleResponseVO listSelectedTables() {
        log.info("获取数据表合集请求");
        
        try {
            Object result = dataSourceService.listSelectedTables();
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("获取数据表合集失败", e);
            return ResponseUtil.failed("获取数据表合集失败：" + e.getMessage());
        }
    }

    /**
     * 刷新当前所关联的数据表
     */
    @PostMapping("/resync-table/{tableIdKey}")
    public SimpleResponseVO resyncTable(@PathVariable String tableIdKey) {
        log.info("刷新数据表请求，表ID：{}", tableIdKey);
        
        try {
            Object result = dataSourceService.resyncTable(tableIdKey);
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("刷新数据表失败", e);
            return ResponseUtil.failed("刷新数据表失败：" + e.getMessage());
        }
    }

    /**
     * 获取数据表下的数据列集合
     */
    @GetMapping("/columns/{tableIdKey}")
    public SimpleResponseVO listColumns(@PathVariable String tableIdKey) {
        log.info("获取数据表列信息请求，表ID：{}", tableIdKey);
        
        try {
            Object result = dataSourceService.listColumns(tableIdKey);
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("获取数据表列信息失败", e);
            return ResponseUtil.failed("获取数据表列信息失败：" + e.getMessage());
        }
    }
} 